package org.six11.olive3.picturephone.server.handler;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import static org.six11.olive3.server.DatabaseThing.*;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.picturephone.server.PPMessageNames;
import org.six11.olive3.server.DatabaseThing;
import org.six11.olive3.server.SqlStrings;
import org.six11.olive3.util.MiscFunctions;
import org.six11.util2.Debug;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class DescriptionRequestMessageHandler extends MessageHandler {

  private static Random entropy = new Random(System.currentTimeMillis());
  private DatabaseThing db;

  public DescriptionRequestMessageHandler(DatabaseThing db) {
    this.db = db;
  }

  public boolean handle(String todoID, String descID, String uid, PrintWriter out) {
    boolean ret = false;
    Message descMsg = new Message(PPMessageNames.MSG_DESC_RES);
    String descString = "<no description available>";
    
    
    Map<String, String> descValues = null;
    try { 
      descValues = db.getRow(SqlStrings.FIND_DESCRIPTION, descID);
      descString = descValues.get("text");
      descID = descValues.get("id");
    } catch (ServletException ex) {
      ex.printStackTrace();
    }

    if (descValues != null && !descValues.isEmpty()) {
      descMsg.addParam(PPMessageNames.PARAM_DESC_TEXT, MiscFunctions.escape(descString));
      descMsg.addParam(PPMessageNames.PARAM_DESC_ID, descID);
      out.write(descMsg.toOliveString());
      try {
        db.update(SqlStrings.DELETE_TODO_DESCRIPTION, todoID);
        ret = true;
      } catch (ServletException ex) {
        ex.printStackTrace();
      }
    } else {
      bug("Could not find info for description " + descID);
//      Message error = new Message(PPMessageNames.MSG_ERROR);
//      error.addParam(PPMessageNames.PARAM_TEXT,
//          "Could not find any more available descriptions for user " + uid + ". Sorry. " + //
//              "This means you have added a description to everything that is available in " + //
//              "the PicturePhone database. More likely than not this is an error and you " + //
//              "should email Gabe (johnsogg@cmu.edu). But if you play all the time it might " + //
//              "be legitimate. I dunno.");
//      out.write(error.toOliveString());
//      bug("Did not find anything. Sent an error message.");
    }
    return ret;
  }
//
//  public void handleNowThisIsOld(Message m, PrintWriter out) {
//    String uid = m.getParamValue(PPMessageNames.PARAM_USER_ID);
//    Message descMsg = new Message(PPMessageNames.MSG_DESC_RES);
//    String descString = "<no description available>";
//    String descID = "<no description available>";
//
//    Map<String, String> todoValues = null;
//    Map<String, String> descValues = null;
//    try {
//      int howMany = db.getInt(SqlStrings.COUNT_TODO_DESCRIBE, uid);
//      if (howMany > 0) {
//        int idx = entropy.nextInt(howMany);
//        todoValues = db.getRow(SqlStrings.FIND_TODO_DESCRIPTION_AT_LIMIT, uid, "" + idx);
//        if (todoValues.get("describe_id") != null) {
//          descValues = db.getRow(SqlStrings.FIND_DESCRIPTION, todoValues.get("describe_id"));
//          descString = descValues.get("text");
//          descID = descValues.get("id");
//        }
//      }
//    } catch (ServletException ex) {
//      ex.printStackTrace();
//    }
//
//    if (descValues != null && !descValues.isEmpty()) {
//      descMsg.addParam(PPMessageNames.PARAM_DESC_TEXT, MiscFunctions.escape(descString));
//      descMsg.addParam(PPMessageNames.PARAM_DESC_ID, descID);
//      out.write(descMsg.toOliveString());
//      try {
//        db.update(SqlStrings.DELETE_TODO_DESCRIPTION, todoValues.get("id"));
//      } catch (ServletException ex) {
//        ex.printStackTrace();
//      }
//    } else {
//      Message error = new Message(PPMessageNames.MSG_ERROR);
//      error.addParam(PPMessageNames.PARAM_TEXT,
//          "Could not find any more available descriptions for user " + uid + ". Sorry. " + //
//              "This means you have added a description to everything that is available in " + //
//              "the PicturePhone database. More likely than not this is an error and you " + //
//              "should email Gabe (johnsogg@cmu.edu). But if you play all the time it might " + //
//              "be legitimate. I dunno.");
//      out.write(error.toOliveString());
//      bug("Did not find anything. Sent an error message.");
//    }
//  }
}
